//
//  MarsHolidayService.m
//  HolidayService
//
//  Created by 안종덕 on 11. 3. 18.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MarsHolidayService.h"
#import "WebService/HolidayService2.h"

@implementation MarsHolidayService

@synthesize countryCode, holidayCode, holiday;
@synthesize countryCodes, holidayCodesOfCountry, holidaysOfCountryStartDateEndDate, holidaysOfCountryYear, holidaysOfCountryYearMonth;
@synthesize yearOfHoliday,monthOfHoliday,startDate,endDate;

// private method start
-(NSInteger)getIntegerValueFromDateByFormat:(NSDate *)aDate stringFormat:(NSString *)aFormat {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:aFormat];
    NSInteger IntegerValue = (NSInteger)[[dateFormatter stringFromDate:aDate] intValue];
    [dateFormatter release];
//    NSLog(@"debug: %@, %d, %@, %@\n", [NSString stringWithUTF8String:__FUNCTION__],IntegerValue, aDate, aFormat);
    return IntegerValue;
}

-(NSDate *)getFirstDateOfYearFromDate:(NSDate *)aDate {
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
    
    NSString *strDate = [[NSString alloc] initWithFormat:@"%d/01/01 00:00:00", [self getIntegerValueFromDateByFormat:aDate stringFormat:@"YYYY"]];
    NSDate *gmtDate  = [outputFormatter dateFromString:strDate] ;
    NSTimeInterval timeZoneOffset = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:gmtDate];
    NSDate *localDate1 = [gmtDate dateByAddingTimeInterval:timeZoneOffset];
    
    [strDate release];
    [outputFormatter release];
    
    NSLog(@"debug:%@, inputDate:%@, outputDate:%@\n", [NSString stringWithUTF8String:__FUNCTION__], aDate, localDate1);
    return localDate1;
}

-(NSDate *)getLastDateOfYearFromDate:(NSDate *)aDate {
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
    
    NSString *strDate = [[NSString alloc] initWithFormat:@"%d/12/31 00:00:00", [self getIntegerValueFromDateByFormat:aDate stringFormat:@"YYYY"]];
    NSDate *gmtDate  = [outputFormatter dateFromString:strDate];
    NSTimeInterval timeZoneOffset = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:gmtDate];
    NSDate *localDate2 = [gmtDate dateByAddingTimeInterval:timeZoneOffset];
    
    [strDate release];
    [outputFormatter release];
    
    NSLog(@"debug:%@, inputDate:%@, outputDate:%@\n", [NSString stringWithUTF8String:__FUNCTION__], aDate, localDate2);
    return localDate2;
}

-(void)wsGetCountriesAvailable {
    HolidayService2_GetCountriesAvailable *request = [[HolidayService2_GetCountriesAvailable new] autorelease];
    
    HolidayService2Soap12BindingResponse *response = [binding GetCountriesAvailableUsingParameters:request];
    NSArray *responseHeaders = response.headers;
    NSArray *responseBodyParts = response.bodyParts;
    for(id header in responseHeaders) {
        // 응답받은 데이터의 헤더정보는 불필요하기 때문에 구현하지 않는다.
    }
    for(id bodyPart in responseBodyParts) {
        if ([bodyPart isKindOfClass:[SOAPFault class]]) {
            NSLog(@"debug(SOAP Fault) : %@\n", ((SOAPFault *)bodyPart).simpleFaultString);
            continue;
        }
        if([bodyPart isKindOfClass:[HolidayService2_GetCountriesAvailableResponse class]]) {
            HolidayService2_GetCountriesAvailableResponse *body = (HolidayService2_GetCountriesAvailableResponse *)bodyPart;
            
            countryCodes = body.GetCountriesAvailableResult.CountryCode;
            
            NSLog(@"debug(%@:%d:%@): %d건의 레코드\n", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithUTF8String:__FUNCTION__],[countryCodes count]);
            /*
             for(id record in countryCodes) {
             if([record isKindOfClass:[HolidayService2_CountryCode class]]) {
             HolidayService2_CountryCode *country = (HolidayService2_CountryCode *)record;
             NSLog(@"debug:%@, %@\n", country.Code, country.Description);
             }
             }
             */
        } 
    }
}

-(void)wsGetHolidaysAvailable:(NSString *)aCountryCode {
    HolidayService2_GetHolidaysAvailable *request = [[HolidayService2_GetHolidaysAvailable new] autorelease];
    request.countryCode = HolidayService2_Country_enumFromString(aCountryCode);
    
    HolidayService2Soap12BindingResponse *response = [binding GetHolidaysAvailableUsingParameters:request];
    NSArray *responseHeaders = response.headers;
    NSArray *responseBodyParts = response.bodyParts;
    for(id header in responseHeaders) {
        // 응답받은 데이터의 헤더정보는 불필요하기 때문에 구현하지 않는다.
    }
    for(id bodyPart in responseBodyParts) {
        if ([bodyPart isKindOfClass:[SOAPFault class]]) {
            NSLog(@"debug(SOAP Fault) : %@\n", ((SOAPFault *)bodyPart).simpleFaultString);
            continue;
        }
        if([bodyPart isKindOfClass:[HolidayService2_GetHolidaysAvailableResponse class]]) {
            HolidayService2_GetHolidaysAvailableResponse *body = (HolidayService2_GetHolidaysAvailableResponse *)bodyPart;
            
            holidayCodesOfCountry = body.GetHolidaysAvailableResult.HolidayCode;
            
            //이하 디버깅용
            NSLog(@"debug(%@:%d:%@): %d건의 레코드\n", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithUTF8String:__FUNCTION__],[holidayCodesOfCountry count]);
        } 
    }
}
-(void)wsGetHolidayDate:(NSString *)aCountryCode holidayCode:(NSString *)aHolidayCode year:(int)anYear {

    HolidayService2_GetHolidayDate *request = [[HolidayService2_GetHolidayDate new] autorelease];
    request.countryCode = HolidayService2_Country_enumFromString(aCountryCode);
    request.holidayCode = aHolidayCode;
    request.year = [[NSNumber alloc] initWithInt:anYear];
    
    HolidayService2Soap12BindingResponse *response = [binding GetHolidayDateUsingParameters:request];
    NSArray *responseHeaders = response.headers;
    NSArray *responseBodyParts = response.bodyParts;
    for(id header in responseHeaders) {
        // 응답받은 데이터의 헤더정보는 불필요하기 때문에 구현하지 않는다.
    }
    for(id bodyPart in responseBodyParts) {
        if ([bodyPart isKindOfClass:[SOAPFault class]]) {
            NSLog(@"debug(SOAP Fault) : %@\n", ((SOAPFault *)bodyPart).simpleFaultString);
            continue;
        }
        if([bodyPart isKindOfClass:[HolidayService2_GetHolidayDateResponse class]]) {
            HolidayService2_GetHolidayDateResponse *body = (HolidayService2_GetHolidayDateResponse *)bodyPart;
            
            dateOfCountryHolidayCodeYear = body.GetHolidayDateResult;
            
            //이하 디버깅용
            NSLog(@"debug(%@:%d:%@):%@를 가져옮\n", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithUTF8String:__FUNCTION__], dateOfCountryHolidayCodeYear);
        } 
    }
}
-(void)wsGetHolidaysForDateRange:(NSString *)aCountryCode startDate:(NSDate *)aStartDate endDate:(NSDate *)anEndDate {
    
    HolidayService2_GetHolidaysForDateRange *request = [[HolidayService2_GetHolidaysForDateRange new] autorelease];
    request.countryCode = HolidayService2_Country_enumFromString(aCountryCode);
    request.startDate = self.startDate;
    request.endDate = self.endDate;
    
    HolidayService2Soap12BindingResponse *response = [binding GetHolidaysForDateRangeUsingParameters:request];
    
    NSArray *responseHeaders = response.headers;
    NSArray *responseBodyParts = response.bodyParts;
    
    for(id header in responseHeaders) {
        // 응답받은 데이터의 헤더정보는 불필요하기 때문에 구현하지 않는다.
    }
    for(id bodyPart in responseBodyParts) {
        if ([bodyPart isKindOfClass:[SOAPFault class]]) {
            NSLog(@"debug(SOAP Fault) : %@\n", ((SOAPFault *)bodyPart).simpleFaultString);
        
            continue;
        }
        if([bodyPart isKindOfClass:[HolidayService2_GetHolidaysForDateRangeResponse class]]) {
            HolidayService2_GetHolidaysForDateRangeResponse *body = (HolidayService2_GetHolidaysForDateRangeResponse *)bodyPart;
            
            holidaysOfCountryStartDateEndDate = body.GetHolidaysForDateRangeResult.Holiday;
            
            //이하 디버깅용
            NSLog(@"debug(%@:%d:%@)\n", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithUTF8String:__FUNCTION__]);
        } 
    }
}

-(void)wsGetHolidaysForYear:(NSString *)aCountryCode year:(int)anYear {
    
    HolidayService2_GetHolidaysForYear *request = [[HolidayService2_GetHolidaysForYear new] autorelease];
    request.countryCode = HolidayService2_Country_enumFromString(aCountryCode);
    request.year = self.yearOfHoliday;
    
    HolidayService2Soap12BindingResponse *response = [binding GetHolidaysForYearUsingParameters:request];
    
    NSArray *responseHeaders = response.headers;
    NSArray *responseBodyParts = response.bodyParts;
    
    for(id header in responseHeaders) {
        // 응답받은 데이터의 헤더정보는 불필요하기 때문에 구현하지 않는다.
    }
    for(id bodyPart in responseBodyParts) {
        if ([bodyPart isKindOfClass:[SOAPFault class]]) {
            NSLog(@"debug(SOAP Fault) : %@\n", ((SOAPFault *)bodyPart).simpleFaultString);
            
            continue;
        }
        if([bodyPart isKindOfClass:[HolidayService2_GetHolidaysForYearResponse class]]) {
            HolidayService2_GetHolidaysForYearResponse *body = (HolidayService2_GetHolidaysForYearResponse *)bodyPart;
            
            holidaysOfCountryYear = body.GetHolidaysForYearResult.Holiday;
            
            //이하 디버깅용
            NSLog(@"debug(%@:%d:%@), %d개의 레코드를 가져옮\n", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithUTF8String:__FUNCTION__], [self.holidaysOfCountryYear count]);
        } 
    }
}

-(void)wsGetHolidaysForMonth:(NSString *)aCountryCode year:(int)anYear month:(int)aMonth {
    
    HolidayService2_GetHolidaysForMonth *request = [[HolidayService2_GetHolidaysForMonth new] autorelease];
    request.countryCode = HolidayService2_Country_enumFromString(aCountryCode);
    request.year = self.yearOfHoliday;
    request.month = self.monthOfHoliday;
    
    HolidayService2Soap12BindingResponse *response = [binding GetHolidaysForMonthUsingParameters:request];
    
    NSArray *responseHeaders = response.headers;
    NSArray *responseBodyParts = response.bodyParts;
    
    for(id header in responseHeaders) {
        // 응답받은 데이터의 헤더정보는 불필요하기 때문에 구현하지 않는다.
    }
    for(id bodyPart in responseBodyParts) {
        if ([bodyPart isKindOfClass:[SOAPFault class]]) {
            NSLog(@"debug(SOAP Fault) : %@\n", ((SOAPFault *)bodyPart).simpleFaultString);
            
            continue;
        }
        if([bodyPart isKindOfClass:[HolidayService2_GetHolidaysForMonthResponse class]]) {
            HolidayService2_GetHolidaysForMonthResponse *body = (HolidayService2_GetHolidaysForMonthResponse *)bodyPart;
            
            holidaysOfCountryYearMonth = body.GetHolidaysForMonthResult.Holiday;
            
            //이하 디버깅용
            NSLog(@"debug(%@:%d:%@, %d건의 레코드를 가져옮)\n", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithUTF8String:__FUNCTION__], [self.holidaysOfCountryYearMonth count]);
        } 
    }
}

-(void)initializeFromWebService {
    [self wsGetCountriesAvailable];    
    self.countryCode = [self.countryCodes objectAtIndex:0];
    
    [self wsGetHolidaysAvailable:self.countryCode.Code];
    self.holidayCode = [self.holidayCodesOfCountry objectAtIndex:0];

//    [self wsGetHolidayDate:self.countryCode.Code holidayCode:self.holidayCode.Code year:[self.yearOfHoliday intValue]];

//    [self wsGetHolidaysForDateRange:countryCode.Code startDate:self.startDate endDate:self.endDate];
    
    [self wsGetHolidaysForYear:self.countryCode.Code year:[self.yearOfHoliday intValue]];
    [self wsGetHolidaysForMonth:self.countryCode.Code year:[self.yearOfHoliday intValue] month:[self.monthOfHoliday intValue]];
}


-(id)init {
    [super init];

    binding = [[HolidayService2 HolidayService2Soap12Binding] retain];
    binding.logXMLInOut = NO;

    self.yearOfHoliday = [[NSNumber alloc] initWithInt:[self getIntegerValueFromDateByFormat:[NSDate date] stringFormat:@"YYYY"]];
    self.monthOfHoliday = [[NSNumber alloc] initWithInt:[self getIntegerValueFromDateByFormat:[NSDate date] stringFormat:@"MM"]];
    
    self.startDate = [self getFirstDateOfYearFromDate:[NSDate date]]; 
    self.endDate = [self getLastDateOfYearFromDate:[NSDate date]];
        
    [self initializeFromWebService];
    
    return self;
}

-(void)dealloc {
    [super dealloc];
    
    [binding release];
    
    [countryCode release];
    [holidayCode release];
    [holiday release];
    
    [yearOfHoliday release];
    [monthOfHoliday release];
    
    [startDate release];
    [endDate release];
    [dateOfCountryHolidayCodeYear release];
    
    [countryCodes release];
    [holidayCodesOfCountry release];
    [holidaysOfCountryStartDateEndDate release];
    [holidaysOfCountryYear release];
    [holidaysOfCountryYearMonth release];
}
@end
